<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The Registry class can globally save and get variables withouth saving them permenantly.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Library
 */
class Dmf_Registry
{
    /**
     * All memory
     * 
     * @var array 
     */
    private static $_staticMemory = array();
    
    /**
     * All memory
     * 
     * @var array 
     */
    private $_memory = array();
    
    /**
     * Registry name
     * 
     * @var string 
     */
    private $_name;
    
    /**
     * All instances
     * 
     * @var array
     */
    private static $_instances = array();
    
    /**
     * Constructor
     * 
     * @param string $name 
     */
    public function __construct( $name )
    {
        self::$_instances[ $name ] = $this;
    }
    
    /**
     * Get an instance
     * 
     * @param string $name
     * @return Dmf_Registry 
     */
    public static function getInstance( $name )
    {
        return ( isset( self::$_instances[ $name ] ) ? self::$_instances[ $name ] : null );
    }
    
    /**
     * Set a variable
     * 
     * @param string $index
     * @param mixed $value 
     */
    public static function set( $index, $value )
    {
        self::$_staticMemory[ $index ] = $value;
    }
    
    /**
     * Get variable
     * 
     * @param string $index
     * @return mixed 
     */
    public static function get( $index )
    {
        return isset( self::$_staticMemory[ $index ] ) ? self::$_staticMemory[ $index ] : null;
    }
    
    /**
     * Set a variable
     * 
     * @param string $index
     * @param mixed $value 
     */
    public function __set( $index, $value )
    {
        $this->_memory[ $index ] = $value;
        
        $this->__construct( $this->_name );
    }
    
    /**
     * Get variable
     * 
     * @param string $index
     * @return mixed 
     */
    public function __get( $index )
    {
        return $this->_memory[ $index ];
        
        $this->__construct( $this->_name );
    }
    
    /**
     * Clear all saved data 
     */
    public static function clear()
    {
        self::$_staticMemory = array();
    }
    
    /**
     * Is the variable set?
     * 
     * @param string $index 
     * @return boolean
     */
    public function contains( $index )
    {
        return isset( self::$_memory[ $index ] ) ? true : false;
    }
}